package ex1;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class MainFrame extends JFrame {

	private JTextField txtName = new JTextField(15);
	private DefaultListModel lmTxtName = new DefaultListModel();

	public MainFrame() {
		this.setSize(400, 300);
		this.setLocation(100,100);
		this.setLayout(new BorderLayout());
	
		
		// North panel ----
		JPanel northPanel = new JPanel(new FlowLayout());
		
		northPanel.add(new JLabel("Name:"));
		
		northPanel.add(txtName);
		
		JButton addButton = new JButton("Add");
		addButton.addActionListener(new AddButtonController());
		northPanel.add(addButton);
		
		this.add(northPanel, BorderLayout.NORTH);
		// ----------------
		
		
		// Center layout --
		JPanel centerPanel = new JPanel(new BorderLayout());
		
		//----Panel----+----Scollbars--+----List-----------+++
		centerPanel.add(new JScrollPane(new JList(lmTxtName)));

		this.add(centerPanel, BorderLayout.CENTER);
		// ----------------
		
		
		// South panel ----
		JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		
		JButton openButton = new JButton("Open");
		southPanel.add(openButton);

		JButton saveButton = new JButton("Save");
		southPanel.add(saveButton);
		
		this.add(southPanel, BorderLayout.SOUTH);
		// ----------------
	}

	private class AddButtonController implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			lmTxtName.addElement(txtName.getText());
		}
		
	}
}
